/**
 * 
 */
package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import tp.pr5.RobotEngineObserver;
import tp.pr5.Rotation;
import tp.pr5.items.InventoryObserver;
import tp.pr5.items.Item;

/**
 * @author VICTOR
 *
 */
public class RobotPanel extends JPanel implements RobotEngineObserver, InventoryObserver{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Item> items;
	private JTable table;
	private TableModel tableModel;
	private JComboBox<Rotation> rotationCombo;
	private JTextField idTextField;
	private JLabel fuel;
	private JLabel garbage;
	private InfoPanel infoPanel;

	/**
	 * constructor
	 * @param MainWindow mainWindow
	 */
	public RobotPanel(MainWindow mainWindow)
	{
		this.infoPanel = mainWindow.getInfoPanel();
		this.items = new ArrayList<Item>();
		build();
	}
	
	private void build()
	{
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createTitledBorder("Robot Info"));
		this.setLayout(new BorderLayout());

		JPanel north = new JPanel();
		JLabel fuelLabel = new JLabel("Fuel: ");
		this.fuel = new JLabel();
		JLabel garbageLabel = new JLabel("Garbage: ");
		this.garbage = new JLabel();
		north.add(fuelLabel);
		north.add(this.fuel);
		north.add(garbageLabel);
		north.add(this.garbage);
		
		this.add(north,BorderLayout.NORTH);
		
		
		this.tableModel = new TableModel();
		this.table = new JTable(tableModel);

		this.table.setRowSelectionAllowed(true);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.table.setPreferredScrollableViewportSize(new Dimension(0,80));

		JScrollPane scrollTable = new JScrollPane(table);
		
		this.add(scrollTable,BorderLayout.CENTER);
	}
	

	/**
	 * return the table
	 * @return JTable table
	 */
	public JTable getTable()
	{
		return this.table;
	}
	
	/**
	 * return rotationCombo
	 * @return rotationCombo
	 */
	public JComboBox<Rotation> getRotationCombo()
	{
		return this.rotationCombo;
	}
	
	/**
	 * return idTextField
	 * @return idTextField
	 */
	public JTextField getIdTextField()
	{
		return this.idTextField;
	}
	
	class TableModel extends AbstractTableModel{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public int getColumnCount() {
			return 2;
		}
		
		@Override
		public int getRowCount() {
			return 	items.size();
		}

		@Override
		public Object getValueAt(int row, int col) {
			if(col==0) 
				return items.get(row).getId();
			else 
				return items.get(row).getDescription();
		}
		
		public String getColumnName (int column){
			if(column==0)
				return "Id";
			else
				return "Description";
		}
	}

	@Override
	public void inventoryChange(List<Item> inventory) {
		this.items = inventory;
		this.tableModel.fireTableDataChanged();
	}

	@Override
	public void inventoryScanned(String inventoryDescription) {}

	@Override
	public void itemScanned(String description) {}

	@Override
	public void itemEmpty(String itemName) {
		this.infoPanel.itemEmpty(itemName);
	}

	@Override
	public void raiseError(String msg) {}

	@Override
	public void communicationHelp(String help) {}

	@Override
	public void engineOff(boolean atShip) {}

	@Override
	public void communicationCompleted() {}

	@Override
	public void robotUpdate(int fuel, int recycledMaterial) {
		this.fuel.setText(String.valueOf(fuel));
		this.garbage.setText(String.valueOf(recycledMaterial));
	}

	@Override
	public void robotSays(String message) {}
	
	public String getSelectedItem()
	{
    	int rowIndex = this.getTable().getSelectedRow();
    	if (rowIndex != -1)
    		return (String) this.getTable().getModel().getValueAt(rowIndex, 0);
    	else
    		return null;
    }

}

